
# -*- coding: utf-8 -*-

import json
from datetime import datetime
from config.Config import DATABASE_LOGS
from controllers.BaseController import BaseController

class StatisticsController(BaseController):
	def get(self, *args, **kwargs):
		fechaInicio = self.get_argument("fecini", None)
		fechaFin = self.get_argument("fecfin", None)

		if fechaInicio and fechaFin and fechaInicio != "" and fechaFin != "":
			dateWhere = " datetime >= DATE('%s') AND datetime <= DATE('%s') " % (fechaInicio, fechaFin)
		elif fechaInicio and fechaInicio != "":
			dateWhere = " datetime >= DATE('%s') " % fechaInicio
		elif fechaFin and fechaFin != "":
			dateWhere = " datetime <= DATE('%s') " % fechaFin
		else:
			dateWhere = " id > 0 "

		totalLogs = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s ;""" % dateWhere, ()).fetchone()[0]
		totalRec = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s AND username <> 'none';""" % dateWhere, ()).fetchone()[0]
		totalNoRec = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s AND username = 'none';""" % dateWhere, ()).fetchone()[0]

		totalRec1 = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s AND asknumber = 0 AND username <> 'none';""" % dateWhere, ()).fetchone()[0]
		totalRec2 = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s AND asknumber = 1 AND username <> 'none';""" % dateWhere, ()).fetchone()[0]
		totalRec3 = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s AND asknumber = 2 AND username <> 'none';""" % dateWhere, ()).fetchone()[0]
		totalRec4 = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s AND asknumber = 3 AND username <> 'none';""" % dateWhere, ()).fetchone()[0]
		totalRec5 = DATABASE_LOGS.execute_sql("""SELECT COUNT(*) from logs WHERE %s AND asknumber = 4 AND username <> 'none';""" % dateWhere, ()).fetchone()[0]

		min1, max1 = DATABASE_LOGS.execute_sql("""SELECT min(distance), max(distance) from logs WHERE %s AND asknumber = 0 AND username <> 'none';""" % dateWhere, ()).fetchone()
		min2, max2 = DATABASE_LOGS.execute_sql("""SELECT min(distance), max(distance) from logs WHERE %s AND asknumber = 1 AND username <> 'none';""" % dateWhere, ()).fetchone()
		min3, max3 = DATABASE_LOGS.execute_sql("""SELECT min(distance), max(distance) from logs WHERE %s AND asknumber = 2 AND username <> 'none';""" % dateWhere, ()).fetchone()
		min4, max4 = DATABASE_LOGS.execute_sql("""SELECT min(distance), max(distance) from logs WHERE %s AND asknumber = 3 AND username <> 'none';""" % dateWhere, ()).fetchone()
		min5, max5 = DATABASE_LOGS.execute_sql("""SELECT min(distance), max(distance) from logs WHERE %s AND asknumber = 4 AND username <> 'none';""" % dateWhere, ()).fetchone()

		distances = [dist for dist in DATABASE_LOGS.execute_sql("""SELECT distance from logs WHERE %s AND username <> 'none' ORDER BY datetime ASC;""" % dateWhere, ()).fetchall()]
		minDateStr, maxDateStr = DATABASE_LOGS.execute_sql("""SELECT min(datetime),max(datetime) from logs WHERE %s AND username <> 'none' ORDER BY datetime ASC;""" % dateWhere, ()).fetchone()

		minDate = None
		if minDateStr is not None and len(minDateStr) > 15:
			minDate = datetime.strptime(minDateStr.split(".")[0], "%Y-%m-%d %H:%M:%S")

		maxDate = None
		if maxDateStr is not None and len(maxDateStr) > 15:
			maxDate = datetime.strptime(maxDateStr.split(".")[0], "%Y-%m-%d %H:%M:%S")

		secondsDiff = None
		if minDate and maxDate:
			dateDiff = maxDate - minDate
			secondsDiff = dateDiff.total_seconds()

		dataJson = {
			"totalLogs": totalLogs,
			"totalRec": totalRec,
			"totalNoRec": totalNoRec,

			"totalRec1": totalRec1,
			"totalRec2": totalRec2,
			"totalRec3": totalRec3,
			"totalRec4": totalRec4,
			"totalRec5": totalRec5,

			"min1": min1,
			"min2": min2,
			"min3": min3,
			"min4": min4,
			"min5": min5,

			"max1": max1,
			"max2": max2,
			"max3": max3,
			"max4": max4,
			"max5": max5,

			"distances": distances,
			"intervalDist": secondsDiff * 1000 / len(distances) if secondsDiff and distances > 0 else 20000,
			"minDateDist": minDate.strftime("%Y-%m-%d") if minDate else None,

			"filterDateIni": fechaInicio if fechaInicio and fechaInicio != "" else None,
			"filterDateFin": fechaFin if fechaFin and fechaFin != "" else None
		}

		self.render("statistics.html", jsondata=json.dumps(dataJson), userid=self.get_secure_cookie('userid'))
